﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using Twipler.App;
using Twipler.App.Render;
using Twipler.App.Session;
using Twipler.Data.Access;
using Twipler.Data.Entity.Twipler;
using Twipler.App.DataDoc;

namespace Twipler.WebApp
{
    public class Visitors
    {
        /// <summary>
        /// Login Twitter authenticated user.
        /// </summary>
        /// <param name="doc"></param>
        /// <returns>TwitterId</returns>
        public static int Login(IDataDoc doc)
        {
            if (doc.Status != DataDocStatus.Ok)
                return -1;

            if (Current.Log.IsInfoEnabled)
                Current.Log.InfoFormat("Login screenname:{0}  realname:{1}", doc.Find("/screen_name").Value, doc.Find("/name").Value);
            
            int twitterId = Convert.ToInt32(doc.Find("/id").Value);

            Visitor thisGuy = Factory.Visitor.GetById(twitterId, false) ?? Create(doc);
            if (thisGuy == null)
                return -1;

            thisGuy.Details = doc;

            doc.Add("unfollowed_count", 0);

            App.Current.Visitor = thisGuy;
           
            thisGuy.LastLogin = DateTime.Now;
            thisGuy.LoginCount++;
            
            Factory.Visitor.Save(thisGuy);

            return thisGuy.Id;
        }

        /// <summary>
        /// Create a new user based on User Json.
        /// </summary>
        /// <param name="doc">DataDoc returned from User verification</param>
        /// <returns>New Visitor</returns>
        public static Visitor Create(IDataDoc doc)
        {
            Visitor newbie = new Visitor()
                                 {
                                     ScreenName = (string)doc.Find("/screen_name").Value, 
                                     Id = Convert.ToInt32(doc.Find("/id").Value), 
                                     Name = (string)doc.Find("/name").Value,
                                     Role = (VisitorRole)App.Config.DefaultVisitorType(),
                                     Design = Factory.Design.Default,
                                     Style = Factory.Style.Default
                                 };
             
            Factory.Visitor.Save(newbie);

            return newbie;
        } 
    }
}